﻿using UnityEngine;

namespace Share
{
    [RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
    public class MeshCircle : MonoBehaviour
    {
        public float radius = 1f; // 圆形半径
        public float borderWidth = 0.05f; // 边框宽度
        public int segments = 60; // 分段数
        public Material circleMaterial; // 圆形材质
        public Color fillColor = Color.white; // 填充颜色
        public Color borderColor = Color.white; // 边框颜色
        public bool enableFill = true; // 是否启用填充

        private Mesh mesh;
        private Vector3[] vertices;
        private int[] triangles;
        private Color[] colors;

        void Start()
        {
            GetComponent<MeshFilter>().mesh = mesh = new Mesh();
            mesh.name = "2D Circle Mesh";
            if (circleMaterial != null)
            {
                GetComponent<MeshRenderer>().material = circleMaterial;
            }
            GenerateCircleMesh();
        }

        void GenerateCircleMesh()
        {
            mesh.Clear();

            // 计算顶点数量
            int borderVertexCount = (segments + 1) * 2; // 边框的内外圈顶点
            int fillVertexCount = enableFill ? segments + 1 : 0; // 填充区域的顶点（中心点 + 内圈顶点）
            int totalVertexCount = borderVertexCount + fillVertexCount;

            vertices = new Vector3[totalVertexCount];
            colors = new Color[totalVertexCount];

            // 计算三角形数量
            int borderTriangleCount = segments * 6; // 边框的三角形
            int fillTriangleCount = enableFill ? segments * 3 : 0; // 填充区域的三角形
            int totalTriangleCount = borderTriangleCount + fillTriangleCount;
            triangles = new int[totalTriangleCount];

            float angleStep = 2 * Mathf.PI / segments;

            // 生成边框顶点（外圈和内圈）
            for (int i = 0; i <= segments; i++)
            {
                float angle = i * angleStep;
                float cos = Mathf.Cos(angle);
                float sin = Mathf.Sin(angle);

                // 外圈顶点（边框外缘）- 使用边框颜色
                vertices[i * 2] = new Vector3(cos * radius, sin * radius, 0);
                colors[i * 2] = borderColor;

                // 内圈顶点（边框内缘）- 使用边框颜色
                vertices[i * 2 + 1] = new Vector3(cos * (radius - borderWidth), sin * (radius - borderWidth), 0);
                colors[i * 2 + 1] = borderColor;
            }

            // 生成填充区域顶点（如果需要填充）
            if (enableFill)
            {
                int fillStartIndex = borderVertexCount;
                
                // 中心点 - 使用填充颜色
                vertices[fillStartIndex] = Vector3.zero;
                colors[fillStartIndex] = fillColor;

                // 内圈顶点（与边框内圈顶点相同位置，但使用填充颜色）
                for (int i = 0; i < segments; i++)
                {
                    float angle = i * angleStep;
                    float cos = Mathf.Cos(angle);
                    float sin = Mathf.Sin(angle);

                    vertices[fillStartIndex + 1 + i] = new Vector3(cos * (radius - borderWidth), sin * (radius - borderWidth), 0);
                    colors[fillStartIndex + 1 + i] = fillColor;
                }
            }

            // 生成边框三角形索引
            for (int i = 0; i < segments; i++)
            {
                int baseIndex = i * 6;
                int currentOuter = i * 2;
                int currentInner = i * 2 + 1;
                int nextOuter = (i + 1) * 2;
                int nextInner = (i + 1) * 2 + 1;

                // 第一个三角形（当前外 -> 下一个外 -> 当前内）
                triangles[baseIndex] = currentOuter;
                triangles[baseIndex + 1] = nextOuter;
                triangles[baseIndex + 2] = currentInner;
                // 第二个三角形（当前内 -> 下一个外 -> 下一个内）
                triangles[baseIndex + 3] = currentInner;
                triangles[baseIndex + 4] = nextOuter;
                triangles[baseIndex + 5] = nextInner;
            }

            // 生成填充区域三角形索引（如果需要填充）
            if (enableFill)
            {
                int fillStartIndex = borderVertexCount;
                int triangleStartIndex = borderTriangleCount;

                for (int i = 0; i < segments; i++)
                {
                    int baseIndex = triangleStartIndex + i * 3;
                    int centerIndex = fillStartIndex; // 中心点
                    int currentInner = fillStartIndex + 1 + i; // 当前内圈顶点
                    int nextInner = fillStartIndex + 1 + ((i + 1) % segments); // 下一个内圈顶点

                    // 填充三角形（中心 -> 当前内圈 -> 下一个内圈）
                    triangles[baseIndex] = centerIndex;
                    triangles[baseIndex + 1] = currentInner;
                    triangles[baseIndex + 2] = nextInner;
                }
            }

            mesh.vertices = vertices;
            mesh.triangles = triangles;
            mesh.colors = colors;
            mesh.RecalculateNormals(); // 重算法线
        }

        // 更新圆形参数
        public void UpdateCircle(float newRadius, float newBorderWidth = -1f)
        {
            radius = newRadius;
            if (newBorderWidth >= 0)
            {
                borderWidth = newBorderWidth;
            }
            GenerateCircleMesh();
        }

        // 设置材质
        public void SetMaterial(Material newMaterial)
        {
            circleMaterial = newMaterial;
            if (GetComponent<MeshRenderer>() != null)
            {
                GetComponent<MeshRenderer>().material = circleMaterial;
            }
        }

        // 设置填充颜色
        public void SetFillColor(Color newFillColor)
        {
            fillColor = newFillColor;
            GenerateCircleMesh();
        }

        // 启用/禁用填充
        public void SetFillEnabled(bool enabled)
        {
            enableFill = enabled;
            GenerateCircleMesh();
        }

        // 设置边框颜色
        public void SetBorderColor(Color newBorderColor)
        {
            borderColor = newBorderColor;
            GenerateCircleMesh();
        }

        // 在Inspector中修改参数后，可在编辑模式实时更新网格
        void OnValidate()
        {
            if (mesh != null)
            {
                GenerateCircleMesh();
            }
        }
    }
}
